package com.yungu.swift.api.base.handler;

import com.yungu.swift.api.base.gateway.GatewayHeader;
import com.yungu.swift.api.config.ApplicationConfig;
import com.yungu.swift.api.utils.RsaUtil;
import com.yungu.swift.base.ResponseData;
import com.yungu.swift.utils.JsonUtils;
import com.yungu.swift.utils.ParamUtil;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

import static com.yungu.swift.api.base.ApiErrorEnum.*;

/**
 * @author cuixiuyin
 * <p>
 * api接口前缀处理器 校验 accessToken、sign、noncestr、refreshToken 参数。
 */
public class PreHandler extends HandlerInterceptorAdapter {

    private static final String METHOD_GET = "get";
    private static final String METHOD_POST = "post";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        PrintWriter out = response.getWriter();
        String method = request.getMethod().toLowerCase();
        // 1. 校验请求方式
        if (!METHOD_GET.equals(method) && !METHOD_POST.equals(method)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NO_SUPPORT_REQUEST.getCode(), NO_SUPPORT_REQUEST.getMsg())));
            return false;
        }
        // 2. 校验 accessToken 参数
        String accessToken = request.getHeader(GatewayHeader.accessToken);
        String jwts = request.getHeader(GatewayHeader.jwts);
        if (ParamUtil.isIllegal(accessToken) && ParamUtil.isIllegal(jwts)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NOT_LOGIN.getCode(), NOT_LOGIN.getMsg())));
            return false;
        }
        // 3. 校验 sign 参数
        String sign = request.getHeader(GatewayHeader.sign);
        if (ParamUtil.isIllegal(sign)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NOT_SIGN.getCode(), NOT_SIGN.getMsg())));
            return false;
        }
        // 4. 校验 noncestr 参数
        String noncestr = request.getHeader(GatewayHeader.noncestr);
        if (ParamUtil.isIllegal(noncestr)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NOT_NONCESTR.getCode(), NOT_NONCESTR.getMsg())));
            return false;
        }
        // 5.  校验rid 参数
        String rid = request.getHeader(GatewayHeader.rid);
        if (ParamUtil.isIllegal(rid)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NOT_RID.getCode(), NOT_RID.getMsg())));
            return false;
        }
        // 6. 校验 appid 参数
        String appid = request.getHeader(GatewayHeader.appid);
        if (ParamUtil.isIllegal(appid)) {
            outPrint(out, JsonUtils.writeJsonStr(ResponseData.buildErrorResponse(NOT_APPID.getCode(), NOT_APPID.getMsg())));
            return false;
        }
        response.reset();
        return super.preHandle(request, response, handler);
    }


    /**
     * 输出错误
     *
     * @param jsonstr
     */
    private void outPrint(PrintWriter out, String jsonstr) {
        if (ApplicationConfig.SERVER_CONFIG.getSafety().getEncrypt()) {
            out.print(RsaUtil.doUrlAndEncrypt(jsonstr));
        } else {
            out.print(jsonstr);
        }
    }
}
